/**
 * All rights Reserved, Designed By 林溪
 * Copyright:    Copyright(C) 2016-2020
 * Company       溪云阁 .
 */

package com.boots.shorturl.common.utils;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.module.boots.exception.CommonRuntimeException;

/**
 * 校验工具类
 * @author：溪云阁
 * @date：2020年6月11日
 */
public class UrlsUtils {

    /**
     * 验证是否是URL
     * @param url
     * @return
     */
    public static boolean verifyUrl(String url) {
        // URL验证规则
        final String regEx = "[a-zA-z]+://[^\\s]*";
        // 编译正则表达式
        final Pattern pattern = Pattern.compile(regEx);
        // 忽略大小写的写法
        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        final Matcher matcher = pattern.matcher(url);
        // 字符串是否与正则表达式相匹配
        final boolean rs = matcher.matches();
        return rs;
    }

    /**
     * 获取域名
     * @author 溪云阁
     * @param address 网址
     * @return String
     * @throws URISyntaxException
     */
    public static String getHost(String address) {
        String host = "";
        try {
            final URL url = new URL(address);
            final URI uri = url.toURI();
            host = uri.getScheme() + "://" + url.getHost();
        }
        catch (final Exception e) {
            throw new CommonRuntimeException(e.fillInStackTrace());
        }
        return host;
    }

}
